<template>
  <div>
    <div v-if="getSystemConfig('NAVYHIS_MODE') !== '1'">
      <a-button :disabled="tableData.length === 0" type="primary" :loading="btnLoading" class="float-right mb-2" @click="onPrintCheck()"> 核对单 </a-button>
    </div>
    <div class="w-full flex">
      <BaseTable
        fill
        class="w-[420px]"
        row-key="inplsh"
        :scroll="{ x: 400 }"
        column-code="001075"
        :loading="loading"
        :data-source="tableData"
        :custom-row="(record) => ({ onClick: () => onSelectRow(record) })"
        :row-class-name="(record) => (record.inplsh === selectLeftRow.inplsh ? 'selected-row' : '')"
        :row-selection="{
          selectedRowKeys,
          onChange: onSelectChange
        }"
      >
        <template #bodyCell="{ column, record }">
          <template v-if="column.dataIndex === 'brName'">
            <div>
              <span>{{ record.brName }}</span>
              <span class="ml-2">{{ record.brSex }}</span>
              <span class="ml-2">{{ record.brAge }}</span>
            </div>
          </template>
        </template>
      </BaseTable>
      <BaseTable
        class="flex-1 ml-2 overflow-hidden"
        fill
        row-key="id"
        :scroll="{ x: 1600 }"
        column-code="001076"
        :loading="rightLoading"
        :data-source="rightTable"
      >
        <template #bodyCell="{ index, column, record }">
          <template v-if="column.dataIndex === 'xh'">
            {{ index + 1 }}
          </template>
          <template v-if="column.dataIndex === 'advStatus'">
            <a-tag color="blue">{{ record.advStatusName || '-' }}</a-tag>
          </template>
          <template v-if="column.dataIndex === 'advExecType'">
            <!-- 医嘱执行类别;0：临时 1：长期 2：出院带药 -->
            <span v-if="record.advExecType == '0'">临时</span>
            <span v-if="record.advExecType == '1'">长期</span>
            <span v-if="record.advExecType == '2'">出院带药</span>
          </template>
          <template v-if="column.dataIndex === 'children'">
            <template v-if="record.advOrderType === 0">
              <DoctorAdviceOrder
                :row-data="record"
                :params-key="{
                  mc: 'adv.mc',
                  gg: 'adv.gg',
                  sl: 'clinicalDose',
                  dw: 'clinicalDoseUnit',
                  yf: 'deliverRoute.fyff',
                  pl: 'fqy.plid',
                  drugPathName: 'drugPathName'
                }"
              />
            </template>
            <template v-else>
              <div class="pl-2">
                <a-tooltip>
                  <template #title>{{ record?.advName }}</template>
                  {{ record?.advName }}
                </a-tooltip>
              </div>
            </template>
          </template>
        </template>
      </BaseTable>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { message } from 'ant-design-vue'
import { post } from '@/utils/request'
import BaseTable from '@/components/BaseTable'
// import BaseButton from '@/components/BaseButton'
import useStore from '../store'

const store = useStore()
const btnLoading = ref(false)
const selectedRowKeys = ref([])
const selectLeftRow = ref({})
const rightTable = ref([])
watch(
  () => store.todaySignData,
  () => {
    selectedRowKeys.value = []
  },
  { immediate: true }
)
const onSelectChange = (keys) => {
  selectedRowKeys.value = keys
}
const { tableData, getData, loading } = useTable({
  url: '/inp/clinicalOrder/getBrxxbChangeResponse',
  pagination: false,
  formatParams: () => {
    return {
      bedCode: store.searchState.bedCode,
      keyword: store.searchState.zjm,
      wardId: store.searchState.bqid
    }
  },
  onSuccess: () => {
    selectLeftRow.value = {}
    rightTable.value = []
    selectedRowKeys.value = []
    if (tableData.value && tableData.value.length > 0) {
      tableData.value.forEach((item) => {
        if (item.todayChangeAdviceNum > 0) selectedRowKeys.value.push(item.inplsh)
      })
    }
  }
})

const onSelectRow = (select) => {
  selectLeftRow.value = select
  getAdvData()
}

const rightLoading = ref(false)
const getAdvData = async () => {
  if (!selectLeftRow.value.inplsh) return message.warning('请选择一名患者')
  rightTable.value = []
  rightLoading.value = true
  const res = await post('/inp/clinicalOrder/getClinicalOrderAllList', {
    inplsh: selectLeftRow.value.inplsh,
    advExecType: store.checkAdvType
    // todayChangeReq: true
  })
  rightLoading.value = false
  if (res.code !== 0) message.error(res.msg || '操作失败')
  rightTable.value = res.data || []
}
const onPrintCheck = async () => {
  btnLoading.value = true
  const { code, msg, data } = await post('/inp/Print/printHDD', { inplshs: selectedRowKeys.value, wardId: store.searchState.bqid })
  btnLoading.value = false
  if (code !== 0) return message.error(msg || '获取打印数据失败')
  window.openExe?.rpt(data) //调用winform打印
}

defineExpose({
  getData,
  getAdvData
})
</script>
